package smetana.modules.business;

import smetana.domain.raw.Agent;
import smetana.modules.business.meta.Commit;
import smetana.util.Pivot;
import static com.google.common.base.Preconditions.*;
import smetana.mapper.raw.AgentMapper;

/**
 * @author dmitry.mamonov
 */
class BusinessAgentImpl extends BaseImpl implements BusinessAgent {
    @Override
    @Commit
    public Agent newAgent(String name) {
        AgentMapper mapper = sql.getMapper(AgentMapper.class);
        Agent agent = new Agent(mapper.nextId());
        agent.setAgentName(name);
        agent.setAddress("");
        mapper.insert(agent);
        return mapper.findById(agent.getId());
    }


    @Override
    public Agent findAgentById(Integer id) {
        if (id == null) {
            return null;
        } else {
            AgentMapper agentMapper = sql.getMapper(AgentMapper.class);
            Agent agent = agentMapper.findById(id);
            return agent;
        }
    }

    @Override
    public void saveAgent(Agent agent) {
        AgentMapper mapper = sql.getMapper(AgentMapper.class);
        mapper.update(checkNotNull(agent));
        sql.commit();
    }

    @Override
    public java.util.List<Agent> listAgents() {
        AgentMapper mapper = sql.getMapper(AgentMapper.class);
        return mapper.listAll();
    }

}
